package com.service.imp;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.MeasureDao;
import com.dao.SuppliersDao;
import com.model.MeasureBean;
import com.model.Messages;
import com.model.SuppliersBean;

@Service
@Transactional(rollbackFor = { RuntimeException.class }, propagation = Propagation.REQUIRED)
public class MeasureService {
	@Autowired
	private MeasureDao measureDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private SuppliersDao suppliersDao;
	private static final Logger logger = Logger.getLogger(MeasureService.class);
	
	
	public List<MeasureBean> list(HttpServletRequest request,String type0) throws Exception{
		String hql="select a from MeasureBean a where 1=1 and a.measure_type=?";
		String type=null;
		try {
			type=request.getParameter("type");
			type=new String(type.getBytes("ISO-8859-1"),"UTF-8");
		} catch (NullPointerException e) {
		}
		if(null!=type&&!"".equals(type)){
			System.out.println(type);
			List<MeasureBean> l=measureDao.find(hql,type);

			return l;
		}else if(null!=type0&&!"".equals(type0)){
			List<MeasureBean> l=measureDao.find(hql,type0);
			return l;
		}else{
			logger.info("查询的type不正确");
			throw new IllegalArgumentException("type 不正确");
		}
	}
	public List<MeasureBean> list(HttpServletRequest request) throws Exception{
		return this.list(request, null);
	}
	public List<MeasureBean> list(String type) throws Exception{
		return this.list(null, type);
	}
	public Messages delete(Long measure_id) {
		Messages m= new Messages();
		this.measureDao.delete(this.measureDao.get(measure_id));
		m.setInfo(true);
		return m;
	}
	public Messages add(MeasureBean measurebean) {
		Messages m= new Messages();
		if(measurebean.getMeasure_name()!=null&&!"".equals(measurebean.getMeasure_name())
				&&measurebean.getMeasure_type()!=null&&!"".equals(measurebean.getMeasure_type())){
			this.measureDao.save(measurebean);
			m.setText("添加成功");
			m.setInfo(true);
		}else{
			m.setInfo(false);
			m.setText("检查到参数不有空值");
		}
		
		return m;
	}
	public Messages edit(MeasureBean measurebean) {
		Messages m= new Messages();
		if(measurebean.getMeasure_id()!=null){
			String sql="update tb_basis_measure a set a.measure_name='"+measurebean.getMeasure_name()
					+ "' where a.measure_id="+measurebean.getMeasure_id();
			this.jdbcTemplate.update(sql);
			m.setText("修改成功");
			m.setInfo(true);
		}
		
		return m;
	}
	public List<SuppliersBean> getSuppliersList() {
		
		return this.suppliersDao.loadAll();
	}
	public Messages addsuppliers(SuppliersBean supliers) {
		Messages m= new Messages();
		this.suppliersDao.save(supliers);
		
		m.setInfo(true);
		m.setText("添加成功");
		return m;
	}
	public Messages editsuppliers(SuppliersBean suppliers) {
		Messages m= new Messages();
		SuppliersBean suppliersBean=this.suppliersDao.get(suppliers.getId());
		suppliersBean.setSuppliers_address(suppliers.getSuppliers_address());
		suppliersBean.setSuppliers_contacts(suppliers.getSuppliers_contacts());
		suppliersBean.setSuppliers_fax(suppliers.getSuppliers_fax());
		suppliersBean.setSuppliers_name(suppliers.getSuppliers_name());
		suppliersBean.setSuppliers_phone(suppliers.getSuppliers_phone());
		this.suppliersDao.update(suppliersBean);
		m.setInfo(true);
		m.setText("修改成功");
		return m;
	}
	public Messages removesuppliers(String id) {
		Messages m= new Messages();
		SuppliersBean suppliersBean=this.suppliersDao.get(id);
		this.suppliersDao.delete(suppliersBean);
		m.setInfo(true);
		m.setText("删除成功");
		return m;
	}
	
}